﻿using DevExpress.XtraBars;
using DevExpress.XtraGrid.Columns;
using LhFormsControlLibrary;
using MDIDemo.PublicClass;
using MDIDemo.PublicSetUp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_BaseAlone : DevExpress.XtraEditors.XtraForm
    {
        public Form_BaseAlone()
        {
            InitializeComponent();
        }
        private GridC gridC = new GridC();
        protected Class_SQLiteOperator class_SQLiteOperator;
        private SaveFileDialog saveFileDialog = new SaveFileDialog();

        #region 虚拟方法
        public virtual void RowCellClickEventOut(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { }
        public virtual void RowCellStyleEventOut(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { }
        public virtual void ViewData(object id) { }
        public virtual void Finder(int PageNo) { }
        public virtual void ToExcel(SaveFileDialog saveFileDialog) { }
        public virtual void ToWord(SaveFileDialog saveFileDialog) { }
        public virtual int AddData()
        {
            return -1;
        }
        public virtual int EditData(object id)
        {
            return -1;
        }
        public virtual int DeleteData(object id)
        {
            return -1;
        }
        #endregion

        #region 保护方法
        protected void SetAdd(string Text, string Hint)
        {
            this.barButtonItem7.Visibility = BarItemVisibility.Always;
            this.barButtonItem7.Caption = Text;
            this.barButtonItem7.Hint = Hint;
        }
        protected void SetEdit(string Text, string Hint)
        {
            this.barButtonItem8.Visibility = BarItemVisibility.Always;
            this.barButtonItem8.Caption = Text;
            this.barButtonItem8.Hint = Hint;
        }
        protected void SetDelete(string Text, string Hint)
        {
            this.barButtonItem9.Visibility = BarItemVisibility.Always;
            this.barButtonItem9.Caption = Text;
            this.barButtonItem9.Hint = Hint;
        }
        protected void SetComponent(string FormText, string GridId)
        {
            this.Text = FormText;
            Class_SetUpBar class_SetUpBar = new Class_SetUpBar();
            class_SetUpBar.pub_SetBar(this.bar1, false);
            class_SQLiteOperator = new Class_SQLiteOperator();
            if (GridId != null)
                SetGrid(GridId);
        }
        protected void EnableOutButton(bool IsEdit)
        {
            if (!IsEdit)
            {
                this.barSubItem2.Visibility = BarItemVisibility.Never;
            }
        }
        protected void EnableViewButton(bool IsEdit)
        {
            if (!IsEdit)
            {
                this.barButtonItem1.Visibility = BarItemVisibility.Never;
            }
        }
        protected void EnableViewButton(bool IsEdit, string ButtonCaption)
        {
            this.barButtonItem1.Caption = ButtonCaption;
            EnableViewButton(IsEdit);
        }
        protected void CloseAddButton()
        {
            this.barButtonItem7.Visibility = BarItemVisibility.Never;
        }
        protected void CloseEditButton()
        {
            this.barButtonItem8.Visibility = BarItemVisibility.Never;
        }
        protected void CloseDeleteButton()
        {
            this.barButtonItem9.Visibility = BarItemVisibility.Never;
        }
        protected void EnableEditButton(bool IsEdit)
        {
            if (!IsEdit)
            {
                this.barButtonItem7.Visibility = BarItemVisibility.Never;
                this.barButtonItem8.Visibility = BarItemVisibility.Never;
                this.barButtonItem9.Visibility = BarItemVisibility.Never;
                this.barButtonItem1.Visibility = BarItemVisibility.Never;
            }
        }
        protected SaveFileDialog GetExcel()
        {
            return GetSave("Excel文件|*.xlsx");
        }
        protected SaveFileDialog GetWord()
        {
            return GetSave("Word文件|*.docx");
        }
        private SaveFileDialog GetSave(string Filter)
        {
            this.saveFileDialog.Filter = Filter;
            return this.saveFileDialog;
        }
        protected void SetViewNo()
        {
            SetButtonNoVisable(this.barButtonItem1);
        }
        protected void SetAddNo()
        {
            SetButtonNoVisable(this.barButtonItem7);
        }
        protected void SetEditNo()
        {
            SetButtonNoVisable(this.barButtonItem8);
        }
        protected void SetDeleteNo()
        {
            SetButtonNoVisable(this.barButtonItem9);
        }
        protected void SetOut(bool isVisible)
        {
            //this.barButtonItem8.Enabled = isVisible;
            //this.barButtonItem9.Enabled = isVisible;
            //this.barButtonItem1.Enabled = isVisible;
            //this.barSubItem2.Enabled = isVisible;
        }
        protected void SetWordNo()
        {
            SetButtonNoVisable(this.barButtonItem12);
        }
        protected void SetExcelNo()
        {
            SetButtonNoVisable(this.barButtonItem11);
        }
        protected string GetOldSql()
        {
            return this.gridPage1.GetPageSql();
        }
        protected void SetGridDataSource(DataTable dataTable)
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                SetOut(true);
                this.gridPage1.SetDataSource(dataTable);
            }
            else
            {
                SetOut(false);
                this.gridPage1.SetDataSource(null);
            }
        }
        protected void SetGrid(string GridId)
        {
            gridC.SetGridC(this.gridPage1.gridControl1
                , this.gridPage1.gridView1
                , "分组区"
                , class_SQLiteOperator.GetGridColumnList(GridId));
        }
        #endregion

        #region 公共方法
        public GridPage GetGridPage()
        {
            return this.gridPage1;
        }
        #endregion

        #region 私有方法
        private void SetButtonNoVisable(BarButtonItem barButtonItem)
        {
            SetButtonNoVisable(barButtonItem, false);
        }
        private void SetButtonNoVisable(BarButtonItem barButtonItem, bool isVisible)
        {
            barButtonItem.Visibility = isVisible ? BarItemVisibility.Always : BarItemVisibility.Never;
        }
        private void barButtonItem11_ItemClick(object sender, ItemClickEventArgs e)
        {
            ToExcel(this.saveFileDialog);
        }
        private void Form_BaseAlone_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.saveFileDialog.Dispose();
        }
        private void barButtonItem12_ItemClick(object sender, ItemClickEventArgs e)
        {
            ToWord(this.saveFileDialog);
        }
        private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
        {
            Finder(this.gridPage1.GetPageNo());
        }
        private void gridPage1_PageChangeEvent(object sender, int PageNo, int Total)
        {
            Finder(PageNo);
        }
        private void barButtonItem9_ItemClick(object sender, ItemClickEventArgs e)
        {
            //DELETE
            int index = this.gridPage1.gridView1.FocusedRowHandle;
            if (index > -1)
            {
                if (MessageBox.Show("您要删除此条数据吗？", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    DataRow dataRow = this.gridPage1.gridView1.GetDataRow(index);
                    if (dataRow != null)
                    {
                        if (DeleteData(dataRow["id"]) > -1)
                        {
                            this.gridPage1.gridView1.DeleteRow(index);
                        }
                    }
                }
            }
        }
        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            //EDIT
            int index = this.gridPage1.gridView1.FocusedRowHandle;
            if (index > -1)
            {
                DataRow dataRow = this.gridPage1.gridView1.GetDataRow(index);
                if (dataRow != null)
                {
                    index = EditData(dataRow["id"]);
                    if (index > -10)
                    {
                        Finder(this.gridPage1.GetPageNo());
                        GridColumn column = this.gridPage1.gridView1.Columns["id"];
                        index = this.gridPage1.gridView1.LocateByValue(0, column, dataRow["id"]);
                        if (index > -1)
                            this.gridPage1.gridView1.FocusedRowHandle = index;
                    }
                }
            }
        }
        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            //INSERT
            if (AddData() > 0 && this.gridPage1.GetPageNo() == 1)
            {
                Finder(this.gridPage1.GetPageNo());
            }
        }
        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            //VIEW
            int index = this.gridPage1.gridView1.FocusedRowHandle;
            if (index > -1)
            {
                DataRow dataRow = this.gridPage1.gridView1.GetDataRow(index);
                if (dataRow != null)
                    ViewData(dataRow["id"]);
            }
        }
        #endregion

        private void Form_BaseAlone_Load(object sender, EventArgs e)
        {
        }

        private void Form_BaseAlone_Shown(object sender, EventArgs e)
        {
            Finder(this.gridPage1.GetPageNo());
        }

        private void gridPage1_RowCellClickEventOut(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            this.RowCellClickEventOut(sender, e);
        }

        private void gridPage1_RowCellStyleEventOut(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            this.RowCellStyleEventOut(sender, e);

        }
    }
}
